package com.app.service.impl;

import com.app.mapper.AdminUserMapper;
import com.app.model.AdminUserModel;
import com.app.model.DividedIntoRecord;
import com.app.model.PlatformChangeRecord;
import com.app.service.AdminUserService;
import com.app.service.IDividedIntoRecordService;
import com.app.service.IPlatformChangeRecordService;
import com.app.utils.StringTool;
import com.app.vo.AdminUserVo;
import com.app.vo.DividedAdminUserInto;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUserModel> implements AdminUserService {

    @Autowired
    private AdminUserMapper adminUserMapper;

    @Autowired
    private IPlatformChangeRecordService platformChangeRecordService;

    @Autowired
    private IDividedIntoRecordService dividedIntoRecordService;

    @Override
    public AdminUserVo getAdminList(Integer page, Integer limit, String keyword) {
        Page<AdminUserModel> page1 = new Page<>(page, limit);
        QueryWrapper<AdminUserModel> qw = new QueryWrapper<>();
        if (StringTool.isNotEmpty(keyword)) {
            qw.like("username", keyword);
            qw.or().like("real_name", keyword);
        }

        Page<AdminUserModel> adminUserPage = adminUserMapper.selectPage(page1, qw);
        List<AdminUserModel> adminUserList = adminUserPage.getRecords();
        for (AdminUserModel adminUserModel : adminUserList) {
            adminUserModel.setPassword(null);
        }
        long count = adminUserPage.getTotal();
        AdminUserVo adminUserVo = new AdminUserVo();
        adminUserVo.setList(adminUserList);
        adminUserVo.setCount((int) count);
        return adminUserVo;
    }

    @Override
    public AdminUserVo getAdminUser(Integer page, Integer limit, Integer roleId) {
        Page<AdminUserModel> page1 = new Page<>(page, limit);
        LambdaQueryWrapper<AdminUserModel> lqw = new LambdaQueryWrapper<>();
        if (roleId != 1) {
            lqw.gt(AdminUserModel::getRoleId, roleId);
        }
        Page<AdminUserModel> adminUserPage = adminUserMapper.selectPage(page1, lqw);
        List<AdminUserModel> adminList = adminUserPage.getRecords();

        long count = adminUserPage.getTotal();
        AdminUserVo vo = new AdminUserVo();
        vo.setList(adminList);
        vo.setCount((int) count);
        return vo;
    }

    //根据账号和密码查管理员用户对象
    @Override
    public AdminUserModel login(String username, String md5Password) {
        LambdaQueryWrapper<AdminUserModel> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AdminUserModel::getUsername, username);
        lqw.eq(AdminUserModel::getPassword, md5Password);
        AdminUserModel adminUserModel = adminUserMapper.selectOne(lqw);
        return adminUserModel;
    }

    /**
     * 查询区域当前用户消费份额
     *
     * @param cityId   城市id
     * @param areaCode 区号
     * @return {@link DividedAdminUserInto}
     */
    @Override
    public DividedAdminUserInto queryRegionCurrentUserConsumptionShare(String cityId, String areaCode) {
        DividedAdminUserInto dividedAdminUserInto = new DividedAdminUserInto();
        //查询市级代理
        AdminUserModel municipalUsers = adminUserMapper.selectUserIdByRoleAndArea(3, cityId);
        if (municipalUsers != null) {
            dividedAdminUserInto.setCityUserId(municipalUsers.getUserId());
            dividedAdminUserInto.setCityId(municipalUsers.getAreaId().toString());
        }
        AdminUserModel districtGeneration = adminUserMapper.selectUserIdByAreaCode(4, areaCode);
        if (districtGeneration != null) {
            dividedAdminUserInto.setAreaUserId(districtGeneration.getUserId());
            // 去除方括号和双引号，并根据逗号分割字符串
            String[] parts = districtGeneration.getAreaCode().replaceAll("[\\[\\]\"]", "").split(",");
            // 获取第三个元素
            String result = parts[2];
            dividedAdminUserInto.setAreaId(result);
        }
        return dividedAdminUserInto;
    }

    /**
     * 为当前平台用户累积积分
     *
     * @param value 值
     * @return boolean
     */
    @Override
    public boolean accumulatePointsForCurrentPlatformUser(Integer userId,String name,BigDecimal value) {
        //写死总平台用户id为2
        AdminUserModel adminUserModel=this.adminUserMapper.selectById(2);
        BigDecimal oldIntegralValue=adminUserModel.getIntegral();
        BigDecimal newIntegralValue=oldIntegralValue.add(value);
        adminUserModel.setIntegral(newIntegralValue);
        this.adminUserMapper.updateById(adminUserModel);
        this.platformChangeRecordService.addPlatformRecord(1,2,userId,name,value,oldIntegralValue,newIntegralValue);
        return true;
    }


    /**
     * 当前平台用户累积券量
     *
     * @param userId 用户id
     * @param name   姓名
     * @param value  值
     * @return boolean
     */
    @Override
    public boolean accumulatVoucherForCurrentPlatformUser(Integer pumpingType,Integer userId,String name,BigDecimal value) {
        //写死总平台用户id为2
        AdminUserModel adminUserModel=this.adminUserMapper.selectById(2);
        if(pumpingType==1)
        {
            BigDecimal oldVoucher=adminUserModel.getVoucher();
            BigDecimal newVoucher=oldVoucher.add(value);
            adminUserModel.setVoucher(newVoucher);
            this.adminUserMapper.updateById(adminUserModel);
            this.platformChangeRecordService.addPlatformRecord(1,1,userId,name,value,oldVoucher,newVoucher);
        }else{
            BigDecimal oldVoucher=adminUserModel.getVoucher();
            BigDecimal newVoucher=oldVoucher.subtract(value);
            adminUserModel.setVoucher(newVoucher);
            this.adminUserMapper.updateById(adminUserModel);
            this.platformChangeRecordService.addPlatformRecord(2,1,userId,name,value,oldVoucher,newVoucher);
        }
        return true;
    }



}
